package org.vaadinetherealintegration;

import com.vaadin.ui.Component;
import com.vaadin.ui.SplitPanel;
import com.vaadin.ui.TabSheet;
import com.vaadin.ui.VerticalLayout;

import java.util.ArrayList;
import java.util.List;

import org.vaadinetherealintegration.client.VMyComponent;

import com.vaadin.terminal.Sizeable;
import com.vaadin.ui.TabSheet.SelectedTabChangeEvent;
import com.vaadin.ui.TabSheet.SelectedTabChangeListener;
import com.vaadin.ui.TabSheet.Tab;

/**
 * 
 * @author Jesus Martinez (jesusrmartinez51@gmail.com)
 *
 */
public class ViewLayout extends VerticalLayout implements SelectedTabChangeListener{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;	
	
	private SplitPanel mainViewSplitPanel = new SplitPanel(
            SplitPanel.ORIENTATION_HORIZONTAL);
	
	private String[] tabs = new String[] { VMyComponent.EXAMPLE1,
			VMyComponent.EXAMPLE2,  VMyComponent.EXAMPLE3 , VMyComponent.EXAMPLE4,
			VMyComponent.EXAMPLE5};
	
	VerticalLayout headBar;

	private TabSheet tabSheet;

	List<VerticalLayout> canvasesLayout = new ArrayList<VerticalLayout>();
	
	MyComponent canvas = null;
	
	public ViewLayout(){
		this.setStyleName("view2d");
		buildMainLayout();		
	}
	
	
	/**
	 * Main Window Layout consists of two components.
	 * First components holds MenuBar plus HeadBar.
	 * Second Component holds SplitPanel with left side bar, and application's
	 * main view.
	 */
	public void buildMainLayout(){
		
		this.setHeight("100%");
		this.setWidth("100%");
		this.setSizeFull();
							
		//Create Layout for top part i.e. HeadBar
		headBar = new HeadBar();
		
		headBar.setVisible(true);
		
		headBar.setSizeFull();
		
		//add headbar to main layout
		this.addComponent(headBar);
				
		//add SplitPanel to main Layout
		this.addComponent(mainViewSplitPanel);
		
		this.setExpandRatio(headBar,1);
		//Gives all extra space to main view split panel
		this.setExpandRatio(mainViewSplitPanel, 5);
		
		//Create individual canvas for each sample to be added
		this.instantiateCanvases();
		
		mainViewSplitPanel.setFirstComponent(createLeftSideBar());	
		mainViewSplitPanel.setSecondComponent(getTabSheet());
		
		//Give left side a minimun of 200 pixels
		mainViewSplitPanel.setSplitPosition(250, Sizeable.UNITS_PIXELS);			
	}

	public void createCanvasTabPanel(){
		tabSheet = new TabSheet();
		tabSheet.setWidth("100%");
		tabSheet.setHeight("100%");
		tabSheet.setSizeFull();
				
		tabSheet.addTab(canvasesLayout.get(0), VMyComponent.EXAMPLE1, null);
		tabSheet.addTab(canvasesLayout.get(1), VMyComponent.EXAMPLE2, null);
		tabSheet.addTab(canvasesLayout.get(2), VMyComponent.EXAMPLE3, null);
		tabSheet.addTab(canvasesLayout.get(3), VMyComponent.EXAMPLE4, null);	
		tabSheet.addTab(canvasesLayout.get(4), VMyComponent.EXAMPLE5, null);
				
		tabSheet.addListener(this);
				
	}
	
	private TabSheet getTabSheet(){
		if(tabSheet == null){
			createCanvasTabPanel();
		}
		
		return tabSheet;
	}
	public void instantiateCanvases(){
		for(int i =0; i<tabs.length ; i++){
			System.out.println("Instantiating Canvas for " + tabs[i]);
			VerticalLayout canvasLayout= new VerticalLayout();
			canvasLayout.setSizeFull();
			
			if(tabs[i].equals(VMyComponent.EXAMPLE1)){
				
				//Initialize First Canvas to be default 
				createCanvas(tabs[i]);
				
				canvasLayout.addComponent(canvas);
			}
			
			canvasesLayout.add(canvasLayout);
		}
	}
	
	public void createCanvas(String canvasNumber){
		canvas = new MyComponent(canvasNumber);
		canvas.setSizeFull();
	}

	/**
	 * Initialize layout to represent the left side bar of application.
	 * Layout will be a combination of two splitpanels.
	 * @return
	 */
	public VerticalLayout createLeftSideBar(){
		
		VerticalLayout leftBarLayout = new VerticalLayout();
				
		leftBarLayout.setWidth("100%");
		leftBarLayout.setHeight("100%");
		leftBarLayout.setMargin(true);
		leftBarLayout.setSpacing(false);
		leftBarLayout.setImmediate(true);
		
		leftBarLayout.setSizeFull();
        
        return leftBarLayout;
	}


	public void selectedTabChange(SelectedTabChangeEvent event) {
		TabSheet tabsheet = event.getTabSheet();

		Tab tab = tabsheet.getTab(tabsheet.getSelectedTab());
		if (tab != null) {
			getWindow().showNotification("Selected tab: " + tab.getCaption());
			VerticalLayout canvasLayout = (VerticalLayout) tab.getComponent();

			canvasLayout.addComponent(canvas);

			canvas.fireEvent(VMyComponent.EXAMPLE_NUMBER, tab.getCaption());
		}
	}
}
